import {getCommdityDetail} from '@/api/api' 

export default {
  namespaced:true,
  state: {
    commdityData:{},
    // 当前展示图片
    activeImg:''
  },
  mutations: {
    SET_COMMDITY_LIST(state,value){
      // 将商品信息存入组件内
      state.commdityData = value
      console.log('state.commdityData',state.commdityData)
    },
    SET_ACTIVEIMG(state,imgUrl){
      state.activeImg = imgUrl
    }
  },
  actions: {  
    async getCommdityInfo(context,query){
      const res = await getCommdityDetail(query)
      context.commit('SET_COMMDITY_LIST',res.data.data)
      context.commit('SET_ACTIVEIMG',res.data.data.skuInfo.skuDefaultImg)
    }
  },
  getters:{
    // 避免一开始没有值，组件渲染时报错
    skuInfo:state => state.commdityData.skuInfo || {},
    cateNav:state => {
      // 使用情景，组件在mounted中调用，getters 的属性时，此时state未更新；解构会出错，所以要错误处理！！
      try{
        const {category1Name,category2Name,category3Name} = state.commdityData.categoryView 
        return [category1Name,category2Name,category3Name]
      }
      catch{
        return {}
      }
    },
    spuList:state=> state.commdityData.spuSaleAttrList || []
  }
}
